package com.wash.shoes.controller.pc;

import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wash.shoes.entity.BrandPo;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.mapper.ShopMapper;
import com.wash.shoes.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.domain.dy.request.DyGoodsRequest;
import com.wash.shoes.entity.ShopGoodsPo;
import com.wash.shoes.service.ShopGoodsService;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

/**
 * @author zhengyunpeng
 * @since 2024-08-26
 */
@Api(tags = {"PC-商品"})
@RestController
@Log4j2
@RequestMapping({"/dev-api/shopGoods", "/shopGoods"})
public class ShopGoodsController extends BaseController {

    @Autowired
    ShopGoodsService shopGoodsService;

    @Autowired
    DyGoodsRequest dyGoodsRequest;

    @ApiOperation("获取商品列表")
    @PostMapping("/list")
    public R<Page<ShopGoodsPo>> goodsList(@RequestBody ShopGoodsPo shopGoodsPo) {
        Page<ShopGoodsPo> page = shopGoodsService.goodsList(shopGoodsPo);
        return R.ok(page);
    }
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private BrandService brandService;
    @ApiOperation(value = "修改新增商品信息,有id为修改没有为新增", notes = "增加商品信息,如果店铺绑定了品牌就会同步到品牌下的所有店铺去")
    @PostMapping("/saveUpdate")
    public R<String> add(@RequestBody ShopGoodsPo shopGoodsPo) {
        Assert.notNull(shopGoodsPo.getShopId(), "商铺id不能为空.");
        if (ObjectUtil.isEmpty(shopGoodsPo.getId())) {
            shopGoodsPo.setStatus(1);
            shopGoodsService.save(shopGoodsPo);
            // 保存更新抖音的商品信息
            dyGoodsRequest.saveOrUpdateProduct(shopGoodsPo);
        } else {
            dyGoodsRequest.saveOrUpdateProduct(shopGoodsPo);
            ShopGoodsPo ori = shopGoodsService.getById(shopGoodsPo.getId());
            shopGoodsService.updateById(shopGoodsPo);
            ShopPo shopPo = shopMapper.selectById(shopGoodsPo.getShopId());
            Assert.notNull(shopPo, "店铺不存在,shopId:" + shopGoodsPo.getShopId());
            BrandPo brandPo = brandService.getById(shopPo.getBrandId());


            if (!Objects.equals(ori.getStatus(), shopGoodsPo.getStatus())) {
                // 商品上架
                if (shopGoodsPo.getStatus().equals(1)) {
                    dyGoodsRequest.postOperate(shopGoodsPo.getId(), 1,brandPo.getAccountid());
                } else {
                    // 下线
                    dyGoodsRequest.postOperate(shopGoodsPo.getId(), 2,brandPo.getAccountid());
                }
            }
        }
        return R.ok();
    }

    @ApiOperation("逻辑删除 修改 status为0")
    @PostMapping("/deleteByStatus")
    public R<ShopGoodsPo> deleteByStatus(String theId) {

        ShopGoodsPo shopGoodsPo = shopGoodsService.getById(theId);
        shopGoodsPo.setStatus(0);
        shopGoodsService.updateById(shopGoodsPo);
        // 修改状态为0 ，如果需要判断是否能删除，后期处理
        return R.ok(shopGoodsPo);
    }

    @ApiOperation("获取详情")
    @PostMapping("/getDetailById")
    public R<ShopGoodsPo> getDetailById(String theId) {
        ShopGoodsPo shopGoodsPo = shopGoodsService.getById(theId);
        if(Objects.nonNull(shopGoodsPo.getShopId())){
          ShopPo shopPo=  shopMapper.selectById(shopGoodsPo.getShopId());
          if(Objects.nonNull(shopPo)){
              shopGoodsPo.setChannel(shopPo.getChannel());
              shopGoodsPo.setBrandId(shopPo.getBrandId());
          }
        }

        return R.ok(shopGoodsPo);
    }


}
